import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:qunaimedical/components/primary_button.dart';
import 'package:qunaimedical/utils/toast_util.dart';

/// 字段修改界面
class FieldEditPage extends StatefulWidget {
  final String value;
  final FieldEditType type;

  const FieldEditPage({Key key, this.value, @required this.type})
      : super(key: key);

  @override
  _FieldEditPageState createState() => _FieldEditPageState();
}

enum FieldEditType {
  name,
  years,
  idcard,
}

class _FieldEditPageState extends State<FieldEditPage> {
  String titleText;
  TextEditingController _controller = TextEditingController();

  /// 是否显示删除图标
  bool _hideSuffixIcon = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(titleText)),
      body: ListView(
        children: [
          _buildInput(),
          PrimaryButton(
            '确认',
            onPressed: () {
              String content = _controller.text.trim();
              if (content.isEmpty) {
                ToastUtil.show('内容不能为空！', context);
              } else {
                switch (widget.type) {
                  case FieldEditType.idcard:
                    if (content.length != 18) {
                      ToastUtil.show('请输入有效的身份证号！', context);
                      return;
                    }
                    break;
                  default:
                }
                Navigator.pop(context, content);
              }
            },
          ),
        ],
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    _controller.text = widget.value;
    switch (widget.type) {
      case FieldEditType.name:
        titleText = '更改姓名';
        break;
      case FieldEditType.years:
        titleText = '从业年限';
        break;
      case FieldEditType.idcard:
        titleText = '身份证号';
        break;
    }
  }

  Widget _buildInput() {
    var formatters;
    var keyboardType;
    switch (widget.type) {
      case FieldEditType.name:
        keyboardType = TextInputType.text;
        formatters = [
          LengthLimitingTextInputFormatter(20),
        ];
        break;
      case FieldEditType.years:
        keyboardType = TextInputType.number;
        formatters = [
          FilteringTextInputFormatter.digitsOnly,
          LengthLimitingTextInputFormatter(2),
        ];
        break;
      case FieldEditType.idcard:
        formatters = [
          FilteringTextInputFormatter.digitsOnly,
          LengthLimitingTextInputFormatter(18),
        ];
        break;
    }
    return Container(
      margin: EdgeInsets.only(top: 20, bottom: 20),
      child: TextField(
        autocorrect: false,
        controller: _controller,
        keyboardType: keyboardType,
        inputFormatters: formatters,
        decoration: InputDecoration(
          fillColor: Colors.white,
          contentPadding: EdgeInsets.symmetric(horizontal: 15),
          filled: true,
          enabledBorder: OutlineInputBorder(
            borderRadius: BorderRadius.all(Radius.circular(0)),
            borderSide: BorderSide(color: Colors.transparent),
          ),
          focusedBorder: OutlineInputBorder(
            borderRadius: BorderRadius.all(Radius.circular(0)),
            borderSide: BorderSide(color: Colors.transparent),
          ),
          suffixIcon: Offstage(
            offstage: _hideSuffixIcon,
            child: IconButton(
                icon: Icon(
                  Icons.highlight_off,
                  color: Colors.grey,
                ),
                onPressed: () {
                  _controller.clear();
                }),
          ),
        ),
        onChanged: (value) {
          setState(() {
            _hideSuffixIcon = value.isEmpty;
          });
        },
      ),
    );
  }
}
